package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Problem;
import dao.ProblemDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2023-01-08
 * Time: 16:06
 */

@WebServlet("/problem")
public class ProblemServlet extends HttpServlet {
    // json 的核心类
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置状态码和格式
        resp.setStatus(200);
        resp.setContentType("application/json;charset=utf8");

        ProblemDAO problemDAO = new ProblemDAO();
        // 尝试获取 id 参数，如果能获取到，说明是获取题目详情；如果不能获取到，说明是获取题目列表
        String idString = req.getParameter("id");
        if (idString == null || "".equals(idString)) {
            // 没有获取到 id 字段，查询题目列表
            List<Problem> problems = problemDAO.selectAll();
            // 将 problems 进行转换成 json 结构的字符串
            String respString = objectMapper.writeValueAsString(problems);
            // 将得到的字符串响应回去-设置 HTTP 响应的 body 部分
            resp.getWriter().write(respString);
        } else {
            // 获取到了题目的 id，查询题目详情
            Problem problem = problemDAO.selectOne(Integer.parseInt(idString));
            String respString = objectMapper.writeValueAsString(problem);
            resp.getWriter().write(respString);
        }

    }
}
